package com.example.hello;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

public class ConfigActivity extends AppCompatActivity {
    private static final String TAG = "MoneyActivity";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_config);
        EditText dollor=findViewById(R.id.dollarConfig);
        Log.i(TAG,"dollarnull="+dollor);
        EditText euro=findViewById(R.id.euroConfig);
        Log.i(TAG,"euronull="+euro);
        EditText won=findViewById(R.id.wonConfig);
        Log.i(TAG,"wonnull="+won);

        //处理接收的数据
        Intent intent =getIntent();
        float dollar2=intent.getFloatExtra("dollar_key",0.1f);
        float euro2=intent.getFloatExtra("euro_key",0.1f);
        float won2=intent.getFloatExtra("won_key",0.1f);
        Log.i(TAG,"onCreate:dollar2="+dollar2);
        Log.i(TAG,"onCreate:euro2="+euro2);
        Log.i(TAG,"onCreate:won2="+won2);
        //放入控件中
        dollor.setText(""+dollar2);
        won.setText(""+won2);
        euro.setText(""+euro2);
    }
    public void config1(View bt){
        EditText dollor=findViewById(R.id.dollarConfig);
        EditText euro=findViewById(R.id.euroConfig);
        EditText won=findViewById(R.id.wonConfig);
        //获取用户新输入的数据项
        String newDollar = dollor.getText().toString();
        String newEuro = euro.getText().toString();
        String newWon = won.getText().toString();
        Log.i(TAG,"save:newdollar="+newDollar);
        Log.i(TAG,"save:neweuro="+newEuro);
        Log.i(TAG,"save:newwon="+newWon);



        //放入intent中
        Intent intent=getIntent();
        Bundle bdl=new Bundle();
        bdl.putFloat("key_dollar1",Float.valueOf(newDollar));
        bdl.putFloat("key_euro1",Float.valueOf(newEuro));
        bdl.putFloat("key_won1",Float.valueOf(newWon));
        intent.putExtras(bdl);
        setResult(3,intent);

        //返回到调用窗口
        SharedPreferences sp=getSharedPreferences("myrate",ConfigActivity.MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putFloat("dollar_rate",Float.valueOf(newDollar));
        editor.putFloat("euro_rate",Float.valueOf(newEuro));
        editor.putFloat("won_rate",Float.valueOf(newWon));
        editor.apply();
        finish();
        /*
        Intent config=new Intent(this,MoneyActivity.class)
                .putExtra("dollar_key1",dollar1)
                .putExtra("euro_key1",euro1)
                .putExtra("won_key1",won1);
        Log.i(TAG,"config1:dollarRate="+dollar1);
        Log.i(TAG,"config1:eurorRate="+euro1);
        Log.i(TAG,"config1:wonRate="+won1);

        //Log.i(TAG,"openConfig:dollarRate:",dollarRate);


        startActivity(config);

        */

    }
}